#!/bin/bash
# 
# File: kill-port.sh
# Purpose: 根据端口kill进程
#

cd $(dirname "$0")
. ./common.sh

PORT=$1
exitcode=1
INSTALLDIR=$(cd "$(dirname "$0")"; pwd);
CHECK_NUMBER_SCRIPT=$INSTALLDIR/check-number.sh

if [ -z "$PORT" ] ; then
    warn '需要端口作为参数';
    exitcode=1;
else
	if test -x $CHECK_NUMBER_SCRIPT; then
		$CHECK_NUMBER_SCRIPT $PORT
		if [ $? -eq 0 ]; then
			pids=`netstat -ptan|grep -w 'LISTEN'|grep :${PORT}|awk '{print $7}'|awk -F/ '{print $1}'`;
			if [ ! -n $pids ]; then
				warn '没有端口为'${PORT}'的进程';
				exitcode=1;
			else
				for pid in $pids; do
					kill -9 $pid && exitcode=0 && result 'kill进程'${pid}'成功';
				done
			fi
		else
			warn '执行kill命令失败'
			exitcode=1;
		fi
	else
		warn '文件不存在:'$CHECK_NUMBER_SCRIPT;
	fi
fi

exit $exitcode

# eof
